//
//  AppDelegate.m
//  weibo
//
//  Created by DP on 16/7/3.
//  Copyright © 2016年 DP. All rights reserved.
//

#import "AppDelegate.h"
#import "XMGFirstPageNVc.h"
#import "XMGFirstPageTVc.h"
#import "XMGAddVc.h"
#import "XMGFoundNVc.h"
#import "XMGFoundTVc.h"
#import "XMGMyselfTVc.h"
#import "XMGMyselfNVc.h"
#import "XMGMessageNVc.h"
#import "XMGMessageTVc.h"
#import "XMGTabBarVc.h"
#import "GetWeiboAccount.h"
#import "WBAccount.h"
#import "WeiboLogin.h"
#import <WeiboSDK.h>
#import "WWKTableViewController.h"
#import "XMGAddNVc.h"

#define ScreenW [UIScreen mainScreen].bounds.size.width
#define ScreenH [UIScreen mainScreen].bounds.size.height
@interface AppDelegate ()<WeiboSDKDelegate>
@property (nonatomic, weak) XMGTabBarVc *tableBarVC;
@property (nonatomic, strong) XMGAddVc *addVc;
@end

@implementation AppDelegate
- (XMGAddVc *)addVc {
    if (!_addVc) {
        XMGAddVc *addVc = [[ XMGAddVc alloc]init];
        _addVc = addVc;
    }
    return _addVc;
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    
    [WeiboSDK enableDebugMode:YES];
    [WeiboSDK registerApp:KAppKey];
    
        
    
    self.window = [[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds];
    //主控制器
    XMGTabBarVc *tableBarVC = [[XMGTabBarVc alloc]init];
    self.tableBarVC = tableBarVC;
    tableBarVC.view.backgroundColor = [UIColor whiteColor];
    self.window.rootViewController = tableBarVC;
    
    //1.firstPage
    XMGFirstPageTVc *firstTVc = [[XMGFirstPageTVc alloc]init];
    XMGFirstPageNVc *firstNVc = [[XMGFirstPageNVc alloc]initWithRootViewController:firstTVc];
    firstTVc.tabBarItem.title = @"首页";
    firstTVc.navigationItem.title = @"首页";
    firstTVc.tabBarItem.image = [[UIImage imageNamed:@"tabbar_home_os7"]imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
    firstTVc.tabBarItem.selectedImage = [UIImage imageNamed:@"tabbar_home_selected_os7"];
    
    [firstTVc.tabBarItem setValue:[UIColor orangeColor] forKeyPath:@"TintColor"];
    [tableBarVC addChildViewController:firstNVc];
    
    //2.massage
    XMGMessageTVc *massageTVc = [[XMGMessageTVc alloc]init];
    XMGMessageNVc *massageNVc =[[XMGMessageNVc alloc]initWithRootViewController:massageTVc];
    massageNVc.tabBarItem.title = @"消息";
    massageNVc.navigationItem.title = @"消息";
    [massageNVc.tabBarItem setValue:[UIColor orangeColor] forKeyPath:@"TintColor"];
    massageNVc.tabBarItem.image = [UIImage imageNamed:@"tabbar_message_center_os7"];
    massageNVc.tabBarItem.selectedImage = [UIImage imageNamed:@"tabbar_message_center_selected_os7"];
    [tableBarVC addChildViewController:massageNVc];
    
    //3.Add
    UIViewController *addVc = [[UIViewController alloc]init];
    addVc.tabBarItem.image = [[UIImage imageNamed:@"tabbar_message_center_selected_os7-1"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    addVc.tabBarItem.imageInsets = UIEdgeInsetsMake(6, 0, -6, 0);
    
    [tableBarVC addChildViewController:addVc];
    
    UIButton *btn = [[UIButton alloc]init];
    [btn setImage:[UIImage imageNamed:@"tabbar_compose_icon_add_os7"] forState:UIControlStateNormal];
    [btn setImage:[UIImage imageNamed:@"tabbar_compose_icon_add"] forState:UIControlStateHighlighted];
    btn.frame = CGRectMake(ScreenW *0.5 -25, ScreenH -49, 50, 50);
    [btn addTarget:self action:@selector(Click) forControlEvents:UIControlEventTouchUpInside];
    [tableBarVC.view addSubview:btn];
    
    //4.Found
    XMGFoundTVc *FoundTVc = [[XMGFoundTVc alloc]init];
    XMGFoundNVc *FoundNVc =[[XMGFoundNVc alloc]initWithRootViewController:FoundTVc];
    FoundTVc.tabBarItem.title = @"发现";
    FoundTVc.tabBarItem.image = [UIImage imageNamed:@"tabbar_discover_os7"];
    FoundTVc.tabBarItem.selectedImage = [UIImage imageNamed:@"tabbar_discover_selected_os7"];
   [FoundTVc.tabBarItem setValue:[UIColor orangeColor] forKeyPath:@"TintColor"];
//  设置控制的背景颜色为 groupTableViewBackgroundColor 
    FoundTVc.view.backgroundColor = [UIColor groupTableViewBackgroundColor];
    [tableBarVC addChildViewController:FoundNVc];
    
    
    //5.Myself
//    WWKTableViewController *MyselfTVc = [[WWKTableViewController alloc]init];
//    XMGMyselfNVc *MyselfNVc =[[XMGMyselfNVc alloc]initWithRootViewController:MyselfTVc];
    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"WWKTableViewController" bundle:nil];
    UIViewController *MyselfTVc = [storyboard instantiateInitialViewController];
    
    MyselfTVc.tabBarItem.title = @"我";
    MyselfTVc.navigationItem.title = @"我";
    MyselfTVc.tabBarItem.image = [UIImage imageNamed:@"tabbar_profile_os7"];
    MyselfTVc.tabBarItem.selectedImage = [UIImage imageNamed:@"tabbar_profile_selected_os7"];
    [MyselfTVc.tabBarItem setValue:[UIColor orangeColor] forKeyPath:@"TintColor"];
    [tableBarVC addChildViewController:MyselfTVc];
    [self.window makeKeyAndVisible];
    
    
    return YES;
}

- (void)Click {
    XMGAddVc *addVc = [[XMGAddVc alloc]init];
    XMGAddNVc *addNVc = [[XMGAddNVc alloc]initWithRootViewController:addVc];
    [self.tableBarVC addChildViewController:addNVc];
    [self.tableBarVC.view addSubview:self.addVc.view];
    [[NSNotificationCenter defaultCenter]postNotificationName:@"addBtnClick" object:nil];
}

#pragma mark--- WeiboSDK Delegate
//登录页面结束后的回调
- (void)didReceiveWeiboResponse:(WBBaseResponse *)response {
    if ([response isKindOfClass:WBAuthorizeResponse.class]) {
        
        if ([(WBAuthorizeResponse *)response accessToken]) {
            self.wbToken = [(WBAuthorizeResponse *)response accessToken];
            self.wbRefrshToken = [(WBAuthorizeResponse *)response refreshToken];
            self.wbCurrentID = [(WBAuthorizeResponse *)response userID];
            self.wbExpirationDate = [(WBAuthorizeResponse *)response expirationDate];
            
            NSDictionary *dic = @{@"wbToken":self.wbToken,@"wbRefrshToken":self.wbRefrshToken,@"wbCurrentID":self.wbCurrentID,@"wbExpirationDate":self.wbExpirationDate};
            
            WBAccount *acctount = [WBAccount wbAccountWithDic:dic];
            [GetWeiboAccount saveAccount:acctount];
        }
        else {
            
        }
    }
}

- (void)didReceiveWeiboRequest:(WBBaseRequest *)request {
    
}

#pragma mark---override Appdelegate Method

//#if __IPHONE_OS_VERSION_MAX_ALLOWED > __IPHONE_9_0

- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<NSString *,id> *)options {
    return [WeiboSDK handleOpenURL:url delegate:self];;
}
//#else
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation {
    return [WeiboSDK handleOpenURL:url delegate:self];;
}

- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url {
    return [WeiboSDK handleOpenURL:url delegate:self];;
}
//#endif

- (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

- (void)applicationDidEnterBackground:(UIApplication *)application {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}

- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

@end
